package zen2d.example.sanguo;

import zen2d.*;
import zen2d.SwingWindow;
import zen2d.util.AssertUtil;

import java.awt.*;


public class GuanYu extends Sprite {

    Animation animation;

    Animation animationAttack;

    Animation current;

    Attack attack = null;
    int speed = 5;

    Direction direction;


    public GuanYu(int x, int y, int width, int height) {
        super(x, y, width, height);

        animation = new Animation();
        animation.setKeyFrames(AssertUtil.gifToImages("image/sanguo/guanyu-walk.gif"));
        animation.setDuration(100);

        animationAttack = new Animation();
        animationAttack.setKeyFrames(AssertUtil.gifToImages("image/sanguo/guanyu-k.gif"));
        animationAttack.setDuration(100);
        animationAttack.setPlayMode(Animation.PlayMode.NORMAL);
        current = animation;

        attack = new Attack(x + width, y, 100, 200);
        Zen2d.addSprite(attack);

        direction = Direction.S;
    }

    @Override
    protected void afterEndLive() {

    }

    @Override
    protected void update() {

        int oldX = this.x;
        int oldY = this.y;

        switch (direction) {
            case L: {
                x -= speed;
            }
            break;
            case R: {
                x += speed;
            }
            break;
            case U: {
                y -= speed;
            }
            break;
            case D: {
                y += speed;
            }
            break;
            case S: {

            }
            break;
        }

        XuChu xuChu = Zen2d.getCollisionWithClass(this, XuChu.class);
        if (xuChu != null) {
            if (isSameHorizontal(xuChu)) {
                x = oldX;
                y = oldY;
            }

        }

//        borderDetection();

        if (animationAttack.isFinished()) {
            attack.endLive();
        }

        int realX = x;
        if (Zen2d.getViewportX() != 0) {
            realX -= Zen2d.getViewportX();
        }

        System.out.println(realX);

        int viewWidth = Zen2d.WIDTH;
        if (realX >= viewWidth / 4 * 3) {
            if (direction.R == direction) {

                Zen2d.moveViewport(speed, 0);
            }

        } else if (realX <= viewWidth / 4) {
            if (Direction.L == direction) {
                Zen2d.moveViewport(-speed, 0);
            }
        }

    }

    @Override
    protected void draw(GraphicsContext graphicsContext) {

        graphicsContext.setColor(Color.PINK);
        graphicsContext.drawRect(x, y, width, height, false);
        int y = this.y;
        int width = this.width;
        int height = this.height;
        if (current == animationAttack) {
            y = this.y - 100;
            width = 300;
            height = 300;
        }
        graphicsContext.drawImage(current.getKeyFrame(), x, y, width, height);

        graphicsContext.setColor(Color.PINK);
        graphicsContext.drawRect(0, y + height - HORIZONTAL_SIZE, Zen2d.WIDTH, HORIZONTAL_SIZE, false);
    }

    @Override
    protected void up() {
        if (animationAttack.isStart() && !animationAttack.isFinished()) {
            return;
        }
        direction = Direction.U;
        current = animation;
    }

    @Override
    protected void down() {
        if (animationAttack.isStart() && !animationAttack.isFinished()) {
            return;
        }
        direction = Direction.D;
        current = animation;

    }

    @Override
    protected void left() {
        if (animationAttack.isStart() && !animationAttack.isFinished()) {
            return;
        }
        direction = Direction.L;
        current = animation;
        direction = Direction.L;

    }

    @Override
    protected void right() {
        if (animationAttack.isStart() && !animationAttack.isFinished()) {
            return;
        }
        direction = Direction.R;
        current = animation;

    }

    @Override
    protected void space() {
        if (animationAttack.isStart() && !animationAttack.isFinished()) {
            return;
        }

        direction = Direction.S;
        animationAttack.reset();
        current = animationAttack;

        attack.setLocation(x + width, y);
        attack.show();


    }
}
